package com.zlk.algorithm.huawei.leetcode.array;

import org.junit.Test;

import java.util.Arrays;

/**
 * @program: algorithm
 * @ClassName Code09_rotate
 * @description:轮转数组
 * 给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。
 * @author: slfang
 * @create: 2025-01-03 13:07
 * @Version 1.0
 **/
public class Code09_rotate {


    //nums = [1,2,3,4,5,6], k = 3
    //  [1,2,3] 1
    //
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        if(k>n){
            k = k%n;
        }
        int[] ans = new int[n];
        for (int i = 0; i < n-k; i++) {
            ans[i+k] = nums[i];
        }
        int index = 0;
        for (int i = n-k; i <n ; i++) {
            ans[index++] = nums[i];
        }
        nums=ans;
        System.arraycopy(ans, 0, nums, 0, n);
    }

    @Test
    public void test(){
        int[] nums = new int[]{1,2,3,4,5,6,7};
        rotate(nums,3);
        System.out.println(Arrays.toString(nums));

    }
}
